package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private CoursePubService coursePubService;

    /*
    * SpringMVC对于参数的处理默认使用的QueryString形式，如果使用其他传入参数形式，需要通过注解来表示
    * */
    @PostMapping("course/list")
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      @RequestBody QueryCourseBaseModel model) {

        // 获得请求头中的令牌信息并解析拿到companyid
        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.queryCourseBaselist(params,model,companyId);
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto1(courseBaseVO);

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseDTO.setCompanyId(companyId);

        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //获取公司id
        Long companyId = SecurityUtil.getCompanyId();
        //获取课程基本信息数据
        CourseBaseDTO courseBase = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        return courseBase;
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto1(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO resultDTO = courseBaseService.modifyCourseBase(courseBaseDTO);

        return resultDTO;
    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBaseById(@PathVariable Long courseBaseId) {
        //获得公司id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    @GetMapping("course/preview/{courseBaseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseBaseId,@PathVariable Long companyId) {
        Map<String, Object> map = coursePubService.previewCourse(courseBaseId, companyId);

        ModelAndView modelAndView = new ModelAndView("learing_article",map);

        return modelAndView;
    }

    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        coursePubService.publish(courseBaseId, companyId, false);
    }
}
